package cn.dkdoo.utils;

import java.util.UUID;

public class TestBuiUtils {
    private String desc = UUID.randomUUID().toString();//测试任务描述
    private long times;//测试次数
    private FunctionTest functionTest = null;//自定义目标方法

    private long max;
    private long min;
    private long avg;
    private long sum;
    private StringBuilder res;

    public TestBuiUtils doTest(FunctionTest functionTest) throws Exception {
        if (functionTest == null) {
            throw new RuntimeException("请定义目标方法");
        }
        this.res = new StringBuilder();
        this.max = Long.MIN_VALUE;
        this.min = Long.MAX_VALUE;
        for (int i = 0; i < times; i++) {
            long start = System.currentTimeMillis();
            //执行目标方法
            functionTest.doTest();
            long duration = (System.currentTimeMillis() - start);
            this.res.append(i).append(": ").append(duration).append(";  ");
            this.max = this.max < duration ? duration : this.max;
            this.min = this.min > duration ? duration : this.min;
            this.sum = this.sum + duration;
        }
        this.avg = this.sum / this.times;
        return this;
    }

    public TestBuiUtils doTest() throws Exception {
        this.doTest(this.functionTest);
        return this;
    }

    public void printAll() {
        System.out.println("测试任务: " + this.desc + ">>>\nmax:" + this.max + "; min:" + this.min + "; avg:" + this.avg + "; sum:" + this.sum /*+ ";\ndetail:  " + this.res*/);
    }

    public void printDetail() {
        System.out.println("测试任务: " + this.desc + ">>>\ndetail:" + this.res);
    }

    public void printAvg() {
        System.out.println("测试任务: " + this.desc + ">>>\navg:" + this.avg);
    }

    public StringBuilder getRes() {
        return res;
    }

    public TestBuiUtils desc(String desc) {
        this.desc = desc;
        return this;
    }

    public TestBuiUtils times(long times) {
        this.times = times;
        return this;
    }

    public TestBuiUtils functionTest(FunctionTest functionTest) {
        this.functionTest = functionTest;
        return this;
    }
//
//    public static void main(String[] args) throws Exception{
//        int incre = 9999;
//        final int size = 99999999;
//        int[] a = new int[size];
//        for (int i = 0; i < size; i++) {
//            a[i] = new Random().nextInt(100000);
//        }
//
//        int times = 10;
//        TestBuiUtils testBuiUtils = new TestBuiUtils();
//        testBuiUtils.desc("systemCopy");
//        testBuiUtils.times(times);
//        testBuiUtils.functionTest(() -> {
//            int[] b = new int[a.length + incre];//新数组
//            System.arraycopy(a, 0, b, 0, a.length);
//        });
//        testBuiUtils.doTest();
//        testBuiUtils.printAll();
//
//        new TestBuiUtils().desc("forCopy").times(times).functionTest(() -> {
//            int[] b = new int[a.length + incre];//新数组
//            for (int i = 0; i < size; i++) {
//                b[i] = a[i];
//            }
//        }).doTest().printAll();
//    }
}
